package com.common.controller;


import com.common.service.CURDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.*;

import static com.common.utils.JSONUtil.*;
import static com.common.utils.JSONUtil.getJsonValueBykey;

@RestController
public class CURDController {

    @Autowired
    CURDService curdService;

    // 测试类
    @RequestMapping("/CURDtest")
    public void test(@RequestParam("dataSet") String dataSet) throws IOException {
        System.out.println(dataSet);
    }

    // json封装 查操作
    @GetMapping("/get/{code}")
    public ArrayList getToGrid(@PathVariable("code") String code,HttpServletRequest request) {
        try {
            String jsonBody = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(jsonBody, "tabName");
            String titles=request.getQueryString()==null?"":URLMatch(getQueryMap(request, "UTF-8"),getJsonValueBykey(jsonBody, "titles"));
            return curdService.getToGrid(tabName, titles);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // json封装 删操作
    @GetMapping("/del/{code}")
    public Map dataDelete(@PathVariable("code") String code,HttpServletRequest request) {
        try {
            String jsonBody = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(jsonBody, "tabName");
            String titles=request.getQueryString()==null? "" :URLMatch(getQueryMap(request, "UTF-8"),getJsonValueBykey(jsonBody, "titles"));
            return curdService.dataDelete(tabName, titles);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // json封装 增操作
    @RequestMapping("/saveTab/{code}")
    public Map saveTable(@PathVariable("code") String code, @RequestParam("dataSet") String data) {
        try {
            String walnut = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(walnut, "tabName");
            ArrayList<Map<String, Object>> dataSet = toArrayList("["+data+"]");
            System.out.println("[params]:tabName:" + tabName + ";dataSet:" + dataSet);
            return curdService.saveTable(tabName, dataSet);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    // json封装 增操作
    @RequestMapping("/saveForm/{code}")
    public Map saveForm(@PathVariable("code") String code, @RequestParam("dataSet") String data) {
        try {
            String walnut = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(walnut, "tabName");
            ArrayList<Map<String, Object>> dataSet = toArrayList(data);
            System.out.println("[params]: tabName:" + tabName + ";dataSet:" + dataSet);
            return curdService.saveForm(tabName, dataSet);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 多结果查询
    @GetMapping("/getToGrid")
    public ArrayList getToGrid(String tabName, String titles) {
        try {
            if (titles == null) titles = "";
            System.out.println("tabName:" + tabName + ";titles:" + titles);
            return curdService.getToGrid(tabName, titles);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 单结果查询
    @GetMapping("/getToForm")
    public Map getToForm(String tabName, String titles) {
        try {
            if (titles == null) titles = "";
            System.out.println("tabName:" + tabName + ";titles:" + titles);
            return curdService.getToForm(tabName, titles);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 单 or 多条保存
    @PostMapping("/tableSave")
    public Map tableSave(String tabName, ArrayList<Map<String, Object>> dataSet) {
        try {
            System.out.println("tabName:" + tabName + ";dataSet:" + dataSet);
            return curdService.saveTable(tabName, dataSet);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 单条保存
    @PostMapping("/formSave")
    public Map saveForm(String tabName, ArrayList<Map<String, Object>> dataSet) {
        try {
            System.out.println("tabName:" + tabName + ";dataSet:" + dataSet);
            return curdService.saveForm(tabName, dataSet);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/dataDel")
    public Map dataDelete(String tabName,String titles){
        try {
            return curdService.dataDelete(tabName, titles);
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/userMD5Save")
    public Map<String, Object> userSaveMD5(ArrayList<Map<String,Object>> dataSet){
        try {
            return curdService.userSaveMD5(dataSet);
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
        return null;
    }
}
